package com.seashop.controller;

import com.alibaba.fastjson.JSONArray;
import com.aliyuncs.IAcsClient;
import com.seashop.response.ResponseBase;
import com.seashop.util.MessageSendUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.validation.constraints.NotBlank;
import java.util.HashMap;
import java.util.Map;

/**
 * @author moredln
 * @desc 短信服务
 * @date 2020-09-02 14:14
 */
@RestController
@RequestMapping("/aliyun/message")
@Api(tags = "阿里短信服务")
@Slf4j
public class AliMessageController {

    private static IAcsClient client;

    @GetMapping("/sendMsg")
    @ApiOperation(value = "发送短信")
    @ApiImplicitParams({
            @ApiImplicitParam(value = "电话号码(区号+电话号)", name = "mobile", dataType = "string", required = true),
            @ApiImplicitParam(value = "验证码", name = "code", dataType = "string", required = true),
            @ApiImplicitParam(value = "短信模版", name = "template", dataType = "string", required = true)
    })
    public ResponseBase sendSms(@NotBlank String mobile, @NotBlank String code, @NotBlank String template) {
        Map<String, String> map = new HashMap<>();
        map.put("code", code);
        String content = JSONArray.toJSONString(map);
        boolean FLAG = MessageSendUtil.sendCodeMsg(mobile, content, template);
        if (FLAG)
            return ResponseBase.success();
        else
            return ResponseBase.fail();
    }

}
